<?php

/**
 * Pes for PHP 5.3+
 *
 * Copyright (c) 2013 phpeskill.com (http://www.pescms.com)
 *
 * For the full copyright and license information, please view
 * the file LICENSE.md that was distributed with this source code.
 */

namespace Install\App;

use \Core\Controller\Controller as Controller;
use \Core\Abnormal\Abnormal as Abnormal;
use \Core\Db\Db as Db;

/**
 * PSE前台首页
 * @author LazyPHP
 * @version 1.0
 */
class Index extends Controller {

    public function __construct() {
        $version = explode('.', phpversion());
        if ($version[1] < 3) {
            exit('PESCMS仅支持在PHP 5.3以上版本运行！');
        }
        if (is_file('../Install/Temp/Yes') && ACTION != 'complete') {
            header("HTTP/1.1 403 You don't have permission to access / on this server");
            header("Content-type: text/html; charset=utf-8");
            exit("PESCMS程序已经安装完毕！为了安全，请删除安装目录。");
        }
    }

    /**
     * 安装界面
     */
    public function index() {
        $this->display();
    }

    /**
     * 配置系统信息
     */
    public function config() {
        $agree = $this->is_p('agree', '您没有同意安装协议！程序无法安装.');
        if($agree != '1'){
            $this->error('请勿提交非法值');
        }
        $this->display();
    }

    /**
     * 执行安装
     */
    public function install() {
        echo '请耐心等待安装';
        ob_flush();
        flush();
        $this->is_p('sitetitle', '请填写网站名称');
        $this->is_p('siteurl', '请填写网站域名');
        $this->is_p('account', '请填写管理员帐号');
        $this->is_p('password', '请填写管理员密码');
        $this->is_p('mail', '请填写管理员邮箱');
        $this->is_p('DB_HOST', '请填写数据库域');
        $this->is_p('DB_NAME', '请填写数据库名称');
        $this->is_p('DB_USER', '请填写数据库帐号');
        $this->is_p('DB_PORT', '请填写数据库端口');
        echo '.';
        ob_flush();
        flush();
        $this->updateConfigFile();
        $this->rewriteIndex();
        echo '.';
        ob_flush();
        flush();
        $this->installDb();
        echo '.';
        ob_flush();
        flush();
        $this->updateDb();
        ob_flush();
        flush();
        echo '.安装成功!系统将在3秒后跳转!';
        ob_flush();
        flush();
        sleep(3);
        echo '<script>location.href = "/Install/index.php/App-Index-complete";</script>';
        ob_flush();
        flush();
    }

    /**
     * 安装完毕
     */
    public function complete() {
        $db = Db::__init();
        $db->tableName('option');
        $site = $db->where('id = 1')->find();
        $this->assign('siteurl', $site['value']);
        $this->display();
    }

    /**
     * 更新配置文件
     */
    private function updateConfigFile() {
        $DB_HOST = $this->is_p('DB_HOST', '请填写数据库域');
        $DB_NAME = $this->is_p('DB_NAME', '请填写数据库名称');
        $DB_USER = $this->is_p('DB_USER', '请填写数据库帐号');
        $DB_PWD = $this->p('DB_PWD');
        $DB_PORT = $this->is_p('DB_PORT', '请填写数据库端口');
        //写入数据库信息到安装目录的配置文件
        $fp = fopen('../Install/Config/config.php', 'r+');
        $str = '';
        if ($fp) {
            while (!feof($fp)) {
                $buffer = fgets($fp, 4096);
                //替换文件
                $buffer = str_replace("'DB_HOST'=>''", "'DB_HOST'=>'{$DB_HOST}'", $buffer);
                $buffer = str_replace("'DB_NAME'=>''", "'DB_NAME'=>'{$DB_NAME}'", $buffer);
                $buffer = str_replace("'DB_USER'=>''", "'DB_USER'=>'{$DB_USER}'", $buffer);
                $buffer = str_replace("'DB_PWD'=>''", "'DB_PWD'=>'{$DB_PWD}'", $buffer);
                $buffer = str_replace("'DB_PORT'=>'3306'", "'DB_PORT'=>'{$DB_PORT}'", $buffer);
                $str .= $buffer;
            }
            fclose($fp);
        }
        file_put_contents('../Install/Config/config.php', $str);

        //写入数据库信息到系统的配置文件.
        $fp = fopen('../Config/config.php', 'r+');
        $str = '';
        if ($fp) {
            while (!feof($fp)) {
                $buffer = fgets($fp, 4096);
                //替换文件
                $buffer = str_replace("'DB_HOST'=>''", "'DB_HOST'=>'{$DB_HOST}'", $buffer);
                $buffer = str_replace("'DB_NAME'=>''", "'DB_NAME'=>'{$DB_NAME}'", $buffer);
                $buffer = str_replace("'DB_USER'=>''", "'DB_USER'=>'{$DB_USER}'", $buffer);
                $buffer = str_replace("'DB_PWD'=>''", "'DB_PWD'=>'{$DB_PWD}'", $buffer);
                $buffer = str_replace("'DB_PORT'=>'3306'", "'DB_PORT'=>'{$DB_PORT}'", $buffer);
                $str .= $buffer;
            }
            fclose($fp);
        }

        file_put_contents('../Config/config.php', $str);
    }

    /**
     * 重写项目的Index
     */
    private function rewriteIndex() {
        $str = "<?php
/**
 * 项目入口
 * @author LazyPHP
 * @copyright ©2003-2103 phpeskill.com
 * @license http://www.pescms.com
 * @version 1.0
 */
define('ITEM', 'App');
define('THEME', './Theme');
require './Core/index.php';";
        file_put_contents('../index.php', $str);
    }

    /**
     * 安装数据库
     */
    private function installDb() {
        $db = Db::__init();
        $fh = file('./Db/pes.sql');
        $str = '';
        foreach ($fh as $key => $value) {
            $str .= $value;
        }
        $db->alter($str);
    }

    /**
     * 更新数据库内容
     * 主要为管理员帐号，网站配置信息
     */
    private function updateDb() {
        $db = Db::__init();
        $sitetitle = $this->p('sitetitle');
        $siteurl = $this->p('siteurl');
        $db->tableName('option');
        $db->where('id = 1')->save(array('value' => $siteurl));
        $db->where('id = 2')->save(array('value' => $sitetitle));

        $account = $this->p('account');
        $password = md5(md5($this->p('password')));
        $mail = $this->p('mail');
        $db->tableName('user');
        $db->where('uid = 1')->save(array('account' => $account, 'password' => $password, 'mail' => $mail));

        if (!is_file('../Install/Temp/Yes')) {
            $fh = fopen('../Install/Temp/Yes', "w");
        }
    }

}
